package top.songxs.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.songxs.gmall.product.entity.BaseAttrInfo;
import top.songxs.gmall.product.entity.BaseAttrValue;
import top.songxs.gmall.product.service.BaseAttrInfoService;
import top.songxs.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.stereotype.Service;
import top.songxs.gmall.product.service.BaseAttrValueService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 86176
 * @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
 * @createDate 2023-05-15 20:14:07
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {
    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    private BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> findAttrInfoList(Long c1Id, Long c2Id, Long c3Id) {
        LambdaQueryWrapper<BaseAttrInfo> wrapper = new LambdaQueryWrapper<>();
        if (c1Id != null && c1Id != 0) {
            wrapper.eq(BaseAttrInfo::getCategoryId, c1Id).eq(BaseAttrInfo::getCategoryLevel, 1).or();
        }
        if (c2Id != null && c2Id != 0) {
            wrapper.eq(BaseAttrInfo::getCategoryId, c2Id).eq(BaseAttrInfo::getCategoryLevel, 2).or();
        }
        if (c3Id != null && c3Id != 0) {
            wrapper.eq(BaseAttrInfo::getCategoryId, c3Id).eq(BaseAttrInfo::getCategoryLevel, 3);
        }
        List<BaseAttrInfo> list = this.list(wrapper);
        List<BaseAttrInfo> attrInfoList = baseAttrInfoMapper.findAttrInfoList(c1Id, c2Id, c3Id);//两种方法任选其一
        return attrInfoList;
    }

    @Override
    @Transactional
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() == null) {
            save(baseAttrInfo);
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            //更改流里面的元素
            List<BaseAttrValue> baseAttrValueList = attrValueList.stream()
                    .peek(baseAttrValue -> baseAttrValue.setAttrId(baseAttrInfo.getId()))
                    .collect(Collectors.toList());
            baseAttrValueService.saveBatch(baseAttrValueList);
        } else {
            //平台属性的修改，
            this.updateById(baseAttrInfo);
            //平台属性值的修改
            // 先删除之前对应的所有的平台属性值
            LambdaQueryWrapper<BaseAttrValue> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            baseAttrValueService.remove(wrapper);
            // 然后添加新的平台属性值
            List<BaseAttrValue> list = baseAttrInfo.getAttrValueList();
            list = list.stream().peek(baseAttrValue -> {
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValue.setId(null);
            }).collect(Collectors.toList());

        baseAttrValueService.saveBatch(list);
        }
    }































}




